package core.schedulers;

import core.router.Packet;
import core.router.Router;
import eduni.simanim.Anim_port;
import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_system;
import eduni.simjava.distributions.Sim_negexp_obj;

public class PacketScheduler extends Sim_entity {
	private Sim_port in;
	private Sim_port out;
	private Sim_negexp_obj delay;
	private double meanPacketInterval;
	private double meanPacketSize;
	private String name;
	// utilizacao media
	private double utilizacaoMedia;
	private int contadorMedioUtilizacao;

	private DataKind kind;

	private int counterPackets;
	public Router router;

	public PacketScheduler(String name, int x, int y, String image,
			double meanPacketInterval, double meanPacketSize, long seed,
			double tam) {

		super(name, image, x, y);

		if (name.equals("LAN")) {

			kind = DataKind.LAN;

			in = new Sim_port("In", "port", Anim_port.RIGHT, 60);
			add_port(in);

			out = new Sim_port("Out", "port", Anim_port.RIGHT, 20);
			add_port(out);

		} else {

			kind = DataKind.INTERNET;

			in = new Sim_port("In", "port", Anim_port.LEFT, 20);
			add_port(in);

			out = new Sim_port("Out", "port", Anim_port.LEFT, 60);
			add_port(out);

		}

		delay = new Sim_negexp_obj("Delay", meanPacketInterval, seed);
		add_generator(delay);

		this.utilizacaoMedia = 0;
		this.contadorMedioUtilizacao = 0;

		this.meanPacketSize = meanPacketSize;
		this.meanPacketInterval = meanPacketInterval;
		this.name = name;

		this.counterPackets = 0;
	}

	public void body() {

		while (Sim_system.running()) {

			Packet packet = new Packet(meanPacketSize * 8, name);
			packet.setArrivingTime(Sim_system.clock());

			this.counterPackets++;
			double sample = delay.sample();
			sim_schedule(out, 0.0, 0, packet);
			sim_trace(1, "S Out");
			router.contarPacote();
			sim_pause(sample);

		}

	}

	public void setMeanPacketSize(double meanPacketSize) {
		this.meanPacketSize = meanPacketSize;
	}

	public double getMeanPacketSize() {
		return meanPacketSize;
	}

	public void setMeanPacketInterval(double meanPacketInterval) {
		this.meanPacketInterval = meanPacketInterval;
	}

	public double getMeanPacketInterval() {
		return meanPacketInterval;
	}

	public double getUtilizacaoMedia() {
		return utilizacaoMedia;
	}

	public void setUtilizacaoMedia(double utilizacaoMedia) {
		this.utilizacaoMedia = utilizacaoMedia;
	}

	public int getContadorMedioUtilizacao() {
		return contadorMedioUtilizacao;
	}

	public void setContadorMedioUtilizacao(int contadorMedioUtilizacao) {
		this.contadorMedioUtilizacao = contadorMedioUtilizacao;
	}
}